function get_available_gmaps() {
	create_overlay(null, function(sId, oImg) {
		$.post('/admin/pages_ajax/get_available_gmaps', { iSiteId : $('#site_id').val() }, function(data) {
			if (data.iGoogleMaps == '0') {
				$(oImg).remove();
				$('#' + sId).remove();
				$.prompt('<?php echo addslashes($aMessages['admin_pages_no_gmaps']); ?>');
				return;
				
			}
			
			$(oImg).replaceWith(data.sContent);
			
		}, 'json');
	});
}


function open_media_manager() {
	create_overlay(null, function(sId, oImg, oParams) {
		create_popup('pages_media', 'pages_media_overlay', 760, 560, 'Select media', '/admin/media/popup/tinymce_insert_media', function() {
			$(oImg).remove();
			$('#' + sId).remove();
			$('#pages_media_overlay').show();
		});
	}, {});
}

tinymce_editor_bookmark = false;

function tinymce_store_bookmark() {
	tinymce_editor_bookmark = tinyMCE.activeEditor.selection.getBookmark();
}


function tinymce_insert_media(oObj, iMediaId, sDim) {
	sDim = sDim||'web';
	
	$.post('/admin/media_ajax/get_media_props', { 'iMediaId' : iMediaId }, function(data) {
		
		var sSrc = '/assets/media/' + sDim + '/' + data.media_subfolder + '/' + data.media_id + '.' + data.extension_ext;
		
		if (data.extension_thumb === 'Y') {
			var oImg = document.createElement('img');
			
			$(oImg).attr({
				src: sSrc
			});
			
			var oDiv = document.createElement('div');
			
			$(oDiv).append(oImg);
			
			tinyMCE.activeEditor.selection.moveToBookmark(tinymce_editor_bookmark);
			
			tinyMCE.execCommand('mceInsertContent', false, $(oDiv).html());
			
			$(oDiv).remove();
			
			$('#pages_media_overlay').remove();
		} else {
			
			//var oImg = document.createElement('img');
			
			//$(oImg)
			
			var oA = document.createElement('a');
			
			$(oA).attr({
				href : sSrc
			}).html(data.media_title + '.' + data.extension_ext);
			
			var oDiv = document.createElement('div');
			
			$(oDiv).append(oA);
			
			tinyMCE.activeEditor.selection.moveToBookmark(tinymce_editor_bookmark);
			
			tinyMCE.execCommand('mceInsertContent', false, $(oDiv).html());
			
			$(oDiv).remove();
			
			$('#pages_media_overlay').remove();
		
		}
		
		
		
	}, 'json');
}


function edit_image_properties(oMedia) {
	var sMedia = $(oMedia).attr('src')
	var iMediaWidth = $(oMedia).width();
	var iMediaHeight = $(oMedia).height();
	
	
	var aMedia = sMedia.split('/');
	
	sMedia = aMedia[aMedia.length - 1];
	
	aMedia = sMedia.split('.');
	
	var iMediaId = aMedia[0];
	
	var sMediaAlt = $(oMedia).attr('alt');
	

	create_overlay(null, function(sId, oImg, oParams) {
		$.post('/admin/pages_ajax/edit_media_props', { iMediaId : iMediaId, iMediaWidth : iMediaWidth, iMediaHeight : iMediaHeight, sMediaAlt : sMediaAlt }, function(data) {
			$(oImg).replaceWith(data.sContent);
			
			$('#page_edit_image_button').bind('click', function(e) {
				var iMediaWidth = $('#page_media_width').val();
				var iMediaHeight = $('#page_media_height').val();
				var sMediaAlt = $('#page_media_alt').val();
				
				if (isNaN(iMediaWidth)) {
					$.prompt('<?php echo addslashes($aMessages['admin_pages_invalid_media_dim']); ?>');
					return;
				}
				
				if (isNaN(iMediaHeight)) {
					$.prompt('<?php echo addslashes($aMessages['admin_pages_invalid_media_dim']); ?>');
					return;
				}
				
				
				$(oMedia).attr({
					width: iMediaWidth,
					height: iMediaHeight,
					alt: sMediaAlt
				});
				
				$('#page_edit_image_props').fadeOut(250, function() {
					$(this).remove();
					$('#<?php echo $this->config->item('site_accr'); ?>_overlay').remove();
				});
				
				
			});
			
		}, 'json');
	});
	
	
		
}

$(document).ready(function() {
	
	$('.eps_header_toggle').bind('click', function(){
		var oContent = $(this).parent().next('.eps_content');
		var sDisplay = oContent.css('display');
		switch(sDisplay) {
			case 'none':
				$(this).removeClass('eps_header_toggle_up').addClass('eps_header_toggle_down');
				oContent.css('display', 'block');
				break;
			case 'block':
				$(this).removeClass('eps_header_toggle_down').addClass('eps_header_toggle_up');
				oContent.css('display', 'none');
				break;
		}
	});
	
	$('#page_save').bind('click', function(){
		var oData = {
			'page_data_id' : $('#page_data_id').val(), 
			'page_title' : $('#page_title').val(), 
			'parent_page_id' : $('#parent_page_id').val(), 
			'page_seo_title' : $('#page_seo_title').val(),
			'page_seo_keywords' : $('#page_seo_keywords').val(),
			'page_seo_description' : $('#page_seo_description').val(), 
			'page_content' : tinyMCE.activeEditor.getContent(), 
			'page_banner' : $('#page_banner').val()
		};
		
		$(':checkbox[name="page_menu[]"]').each(
			function(nr) {
				var sMenu = $(this).attr('id').replace('page_menu_', '');
				
				oData['menu_id_' + sMenu] = (($(this).is(':checked') === true) ? 'Y' : 'N');
			}
		);
		
		var components_L = new Array();
		$('#components_L  option:selected').each(function(){
			components_L.push($(this).val());
		});
		
		var components_A = new Array();
		$('#components_A  option:selected').each(function(){
			components_A.push($(this).val());
		});
		
		var components_B = new Array();
		$('#components_B  option:selected').each(function(){
			components_B.push($(this).val());
		});
		
		var components_R = new Array();
		$('#components_R  option:selected').each(function(){
			components_R.push($(this).val());
		});
		
		oData['components_L[]'] = components_L;
		oData['components_A[]'] = components_A;
		oData['components_B[]'] = components_B;
		oData['components_R[]'] = components_R;
		
		$.post('/admin/pages_ajax/save_page', oData, function(data){
			if(data.sError) {
				alert(data.sError);
			} else if(data.iPageDataId) {
				$('#page_data_id').val(data.iPageDataId);
				alert('The page has been saved.');
			}
		}, 'json');
	});
	
	setTimeout(function() {
		tinyMCE.init({
			// General options
			mode : "textareas",
			theme : "advanced",
			width : '715px',
			height : '500px',
		
			plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advgmap,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,spellchecker",
	
			// Theme options
			theme_advanced_buttons1 : "spellchecker,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,<!--gmap,-->cleanup,code,|,insertdate,inserttime,<!--preview,-->|,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,<!--emotions,-->iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs<!--,|,visualchars,nonbreaking,template,pagebreak-->",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
	
			
			relative_urls : false, 
			// Example content CSS (should be your site CSS)
			content_css : "/media/css/main_content/edit",
	
			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js",
			oninit : function() {
				
			}, 
			setup : function(ed) {
				ed.onEvent.add(function(ed){
					tinymce_store_bookmark();
				});
			}
		});
	}, 1000);
});